<template>
  <div>
    <el-dialog width="80%" :visible.sync="dialogVisible" @close="onClose" :title="title">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px"
               label-position="left">
        <el-form-item label="标题" prop="title">
          <el-input  v-model="formData.title"
                     placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="版本号" prop="versionCode">
          <el-input  v-model="formData.versionCode"
                     placeholder="请输入版本号"></el-input>
        </el-form-item>
        <el-form-item  label="版本内容" prop="content">
          <Editor v-model="formData.content" :height="200"></Editor>
        </el-form-item>
        <el-form-item label="Android" prop="downloadUrl">
          <FileWgt :fileSize="100" v-model="formData.downloadUrl"></FileWgt>
        </el-form-item>
        <el-form-item label="ios" prop="iosDownloadUrl">
          <el-input  v-model="formData.iosDownloadUrl"
                     placeholder="请输入IOS链接"></el-input>
        </el-form-item>
        <el-form-item label="更新类型" prop="updateType">
          <el-radio-group v-model="formData.updateType" size="medium">
            <el-radio :label=0>热更</el-radio>
            <el-radio :label=1>强更</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发布状态" prop="status">
          <el-radio-group v-model="formData.status" size="medium">
            <el-radio :label=0>已发布</el-radio>
            <el-radio :label=1>未发布</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {VersionAdd, VersionUpdate,VersionSee} from "@/api/version/version"

export default {
  data() {
    return {
      dialogVisible: false,
      title: '编辑',
      formData: {
        versionCode:undefined,
        updateType:0,
        downloadUrl:undefined,
        iosDownloadUrl:undefined,
        title:undefined,
        content:undefined,
        status:0
      },
      rules: {
        versionCode:[{
          required: true,
          message: '请输入版本号',
          trigger: 'blur'
        }],
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }],
        content: [{
          required: true,
          message: '请输入内容',
          trigger: 'blur'
        }],
        downloadUrl:[{
          required: true,
          message: '请上传安卓APK',
          trigger: 'blur'
        }],
        iosDownloadUrl:[{
          required: true,
          message: '请输入IOS链接',
          trigger: 'blur'
        }],

      },
      id:null,
      ids:null
    }
  },

  methods: {
    async getSee(id){
      let res = await VersionSee({id:id})
      console.log('查看:',res)
      this.formData = res.data
      this.formData.id = this.ids
    },
    // 弹窗
    async onOpen(id) {
      if(id){
        this.ids = id
        this.id = true
        this.title="编辑"
        this.getSee(id)

      }else{
        this.id = false
        this.title="新增"
      }
      this.dialogVisible = true
    },
    onClose() {
      this.dialogVisible = false
      this.$refs['elForm'].resetFields()
      this.formData.destroynumber = undefined
      this.formData.energyPerStamina = undefined
      this.formData.outputenerg = undefined
      this.formData.synthesisRate = undefined
    },
    close() {
      this.dialogVisible = false
    },
    // 提交数据
    handleConfirm() {
      this.$refs['elForm'].validate(async valid => {
        this.loading = true
        if (!valid) {
          // 如果验证不通过，则阻止后续代码执行
          this.loading = false;
          return;
        }
        if (this.id) {
          try {
            // 编辑
            await VersionUpdate(this.formData)
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        } else {
          try {
            // 新增
            await VersionAdd(this.formData)
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        }
      })
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
